package me.algo.tree;

import javax.sound.sampled.Line;
import java.util.LinkedList;
import java.util.Objects;

/**
 * @author guozheng
 * @date 2024/4/20
 */
public class T10 {
    public static void main(String[] args) {

    }

    /**
     * 700. 二叉搜索树中的搜索
     * @param root
     * @param val
     * @return
     */
    public static TreeNode searchBST(TreeNode root, int val) {
        if (Objects.isNull(root)) {
            return null;
        }

        LinkedList<TreeNode> que = new LinkedList<>();
        que.addLast(root);
        while (!que.isEmpty()) {
            int size = que.size();
            while (size > 0) {
                size--;
                TreeNode node = que.removeFirst();
                if (node.val == val) {
                    return node;
                }
                if (node.left != null) {
                    que.addLast(node.left);
                }
                if (node.right != null) {
                    que.addLast(node.right);
                }

            }
        }
        return null;
    }
}
